import AssemblerBase from "../../Common/Assemblers/AssemblerBase";
import TextureGrid, { GridElement } from "./TextureGrid";

const gfx = cc['gfx'];

let vfmtPosUvColor = new gfx.VertexFormat([
    { name: gfx.ATTR_POSITION, type: gfx.ATTR_TYPE_FLOAT32, num: 2 },
    { name: gfx.ATTR_UV0, type: gfx.ATTR_TYPE_FLOAT32, num: 2 },
    { name: gfx.ATTR_COLOR, type: gfx.ATTR_TYPE_UINT8, num: 4, normalize: true },
]);

export default class TextureGridAssembler extends AssemblerBase {
    comp: any = null;
    
    public updateRenderData(comp: cc.RenderComponent) {
        if (comp._vertsDirty) {
            this.updateUVVerts(comp);
            comp._vertsDirty = false;
        }
    }

    /**
     * @override
     * @dec 初始化 `_renderData` 数据
     */
    init(comp: any) {
        super.init(comp);
        this.comp = comp;

        let textureGrid:TextureGrid = this.comp.node.getComponent(TextureGrid);
        this.verticesCount = 4 * textureGrid.xCount * textureGrid.yCount;
        this.indicesCount = 6 * textureGrid.xCount * textureGrid.yCount;

        this._renderData = new cc.RenderData();
        this._renderData.init(this);
        this.initData();
    }

    getVfmt() {
        return vfmtPosUvColor;
    }

    fillBuffers(comp, renderer) {
        let textureGrid = this.comp.node.getComponent(TextureGrid);
        if (textureGrid.isDirty) {
            textureGrid.isDirty = false;
            this.updateUVVerts(comp);
        }
        super.fillBuffers(comp, renderer);

    }

    updateUVVerts(comp: any) {

        let grid = this.comp.node.getComponent(TextureGrid).getGrid();
        let verts = this._renderData.vDatas[0];

        let dstOffset = 0;
        for (let i = 0; i < grid.length - 1; i++) {
            for (let j = 0; j < grid[i].length - 1; j++) {
                let gridElement:GridElement = grid[i][j];
                let quad = gridElement.quad;
                
                verts[dstOffset] = quad.vertexLB.position.x;
                verts[dstOffset + 1] = quad.vertexLB.position.y;
                verts[dstOffset + 2] = quad.vertexLB.uv.x;
                verts[dstOffset + 3] = quad.vertexLB.uv.y;

                dstOffset += this.floatsPerVert;

                verts[dstOffset] = quad.vertexRB.position.x;
                verts[dstOffset + 1] = quad.vertexRB.position.y;
                verts[dstOffset + 2] = quad.vertexRB.uv.x;
                verts[dstOffset + 3] = quad.vertexRB.uv.y;

                dstOffset += this.floatsPerVert;

                verts[dstOffset] = quad.vertexLT.position.x;
                verts[dstOffset + 1] = quad.vertexLT.position.y;
                verts[dstOffset + 2] = quad.vertexLT.uv.x;
                verts[dstOffset + 3] = quad.vertexLT.uv.y;

                dstOffset += this.floatsPerVert;

                verts[dstOffset] = quad.vertexRT.position.x;
                verts[dstOffset + 1] = quad.vertexRT.position.y;
                verts[dstOffset + 2] = quad.vertexRT.uv.x;
                verts[dstOffset + 3] = quad.vertexRT.uv.y;

                dstOffset += this.floatsPerVert;
            }   
        }
    }
}